
import { ref, onMounted, onUnmounted } from 'vue';
import { dateFilter, timeFilter } from '@/utils';

export function clock () {
	let now = new Date();
	const date = ref(dateFilter(now));
	const time = ref(timeFilter(now));
	let task;
	const start = () => {
		task = setInterval(() => {
			now = new Date();
			date.value = dateFilter(now);
			time.value = timeFilter(now);
		}, 1000);
	};
	onMounted(start);
	onUnmounted(() => {
		task && clearInterval(task);
	});
	return {
		date,
		time,
	};
}